package com.zw.stock_sell_storage.interceptor;


import com.zw.stock_sell_storage.dto.UserDto;
import com.zw.stock_sell_storage.dto.ex.UserDtoEx;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 登录拦截
 */
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //进入应用控制器之前进行调用
        System.out.println("preHandle");
        UserDtoEx userDtoEx = (UserDtoEx) request.getSession().getAttribute("user");
        //没有登录
        if (userDtoEx == null){
            //让用户跳转到指定业务
            //response.sendRedirect("login.html");
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        //应用控制器处理完后，前端控制器渲染视图前调用
        //System.out.println("postHandle");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        //前端控制器渲染完视图后进行调用
        //System.out.println("afterCompletion");
    }
}
